package UDP;

public class Test1 {
    public static void main(String[] args) {

        int[] left = {2,3,2,1};
        int[] right = {3,1,2,1};
        System.out.println(findMinimum(4, left, right));
    }

    public static int findMinimum(int n, int[] left, int[] right) {
        int ret = 0;
        int minL = 13;
        int minR = 13;

        for(int i = 0; i < n; i++){
            if(left[i] == 0) {
                ret += right[i];
                right[i] = 0;
            }
            if(right[i] == 0) {
                ret += left[i];
                left[i] = 0;
            }
            if(left[i] != 0){
                minL = Math.min(left[i],minL);
            }
            if(right[i] != 0){
                minR = Math.min(right[i],minR);
            }
        }
        int sumL = 0;
        int sumR = 0;
        int leftCount = 0;
        int rightCount = 0;
        for(int i = 0; i < n; i++){
            if(left[i] > minL){
                sumL += left[i];
            }else if (left[i] == minL){
                leftCount++;
            }
            if(right[i] > minR){
                sumR += right[i];
            }else if (right[i] == minR){
                rightCount++;
            }
        }
        sumL += minL*(leftCount-1);
        sumR += minR*(rightCount-1);
        ret = ret + Math.min(sumL,sumR) + 2;
        return ret;
    }
}
